package com.wayhome.srpingbootmybatis.metrics;

import io.prometheus.client.Collector;
import io.prometheus.client.CollectorRegistry;

import java.util.Enumeration;
import java.util.Set;

public class PrefixFilteredCollectorRegistry extends CollectorRegistry {
    private static PrefixFilteredCollectorRegistry _instance_ = null;
    public PrefixFilteredCollectorRegistry() {
        super();
    }

    public static PrefixFilteredCollectorRegistry getInstance() {
        if (_instance_ == null) {
            synchronized (PrefixFilteredCollectorRegistry.class) {
                if (_instance_ == null) {
                    _instance_ = new PrefixFilteredCollectorRegistry(true);
                }
            }
        }
        return _instance_;
    }


    public PrefixFilteredCollectorRegistry(boolean autoDescribe) {
        super(autoDescribe);
    }

    @Override
    public void register(Collector m) {
        super.register(m);
    }

    @Override
    public void unregister(Collector m) {
        super.unregister(m);
    }

    @Override
    public void clear() {
        super.clear();
    }

    @Override
    public Enumeration<Collector.MetricFamilySamples> metricFamilySamples() {
        return super.metricFamilySamples();
    }

    @Override
    public Enumeration<Collector.MetricFamilySamples> filteredMetricFamilySamples(Set<String> includedNames) {
        if (includedNames == null || includedNames.isEmpty()) {
            return super.filteredMetricFamilySamples(includedNames);
        }
        return new PrefixFilterEnumeration(super.metricFamilySamples(), includedNames);
    }

    @Override
    public Double getSampleValue(String name) {
        return super.getSampleValue(name);
    }

    @Override
    public Double getSampleValue(String name, String[] labelNames, String[] labelValues) {
        return super.getSampleValue(name, labelNames, labelValues);
    }

    class PrefixFilterEnumeration implements Enumeration<Collector.MetricFamilySamples> {

        private Enumeration<Collector.MetricFamilySamples> enumeration;
        private Set<String> includedNames;

        private Collector.MetricFamilySamples nextElement;

        public PrefixFilterEnumeration(Enumeration<Collector.MetricFamilySamples> enumeration, Set<String> includedNames) {
            this.enumeration = enumeration;
            this.includedNames = includedNames;
        }

        @Override
        public boolean hasMoreElements() {
            nextElement = null;
            while (enumeration.hasMoreElements()) {
                Collector.MetricFamilySamples metricFamilySamples = enumeration.nextElement();
                for (String name : includedNames) {
                    if (metricFamilySamples.name.startsWith(name)) {
                        nextElement = metricFamilySamples;
                        return true;
                    }
                }
            }
            return false;
        }

        @Override
        public Collector.MetricFamilySamples nextElement() {
            return nextElement;
        }
    }



}
